package com.rjj.wlqq.wx.common.core;

import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ApiConfigKit
{
    private static final Log log = Log.getLog(ApiConfigKit.class);
    private static final ThreadLocal<String> TL = new ThreadLocal<>();
    private static final Map<String, ApiConfig> CFG_MAP = new ConcurrentHashMap<>();
    private static final String DEFAULT_CFG_KEY = "_default_cfg_key_";
    private static boolean devMode = false;

    public ApiConfigKit()
    {
    }

    public static void setDevMode(boolean devMode)
    {
        ApiConfigKit.devMode = devMode;
    }

    public static boolean isDevMode()
    {
        return devMode;
    }

    public static ApiConfig putApiConfig(ApiConfig apiConfig)
    {
        if (CFG_MAP.size() == 0)
        {
            CFG_MAP.put(DEFAULT_CFG_KEY, apiConfig);
        }

        return CFG_MAP.put(apiConfig.getAppId(), apiConfig);
    }

    public static ApiConfig removeApiConfig(ApiConfig apiConfig)
    {
        return removeApiConfig(apiConfig.getAppId());
    }

    public static ApiConfig removeApiConfig(String appId)
    {
        return CFG_MAP.remove(appId);
    }

    public static void setThreadLocalAppId(String appId)
    {
        if (StrKit.isBlank(appId))
        {
            appId = CFG_MAP.get(DEFAULT_CFG_KEY).getAppId();
        }

        TL.set(appId);
    }

    public static void removeThreadLocalAppId()
    {
        TL.remove();
    }

    public static String getAppId()
    {
        String appId = TL.get();
        if (StrKit.isBlank(appId))
        {
            appId = CFG_MAP.get(DEFAULT_CFG_KEY).getAppId();
        }

        return appId;
    }

    public static ApiConfig getApiConfig()
    {
        String appId = getAppId();
        return getApiConfig(appId);
    }

    public static ApiConfig getApiConfig(String appId)
    {
        log.debug("appId: " + appId);
        ApiConfig cfg = CFG_MAP.get(appId);
        if (cfg == null)
        {
            throw new IllegalStateException("需事先调用 ApiConfigKit.putApiConfig(apiConfig) 将 appId对应的 ApiConfig 对象存入，如JFinalConfig.afterJFinalStart()中调用, 才可以使用 ApiConfigKit.getApiConfig() 系列方法");
        } else
        {
            return cfg;
        }
    }

}
